/**
 * @author floyd
 */

//根据ID获取对应的dom元素
function $(obj){
	return typeof obj == 'string'?document.getElementById(obj):obj;
}
//获取某dom元素name对应的css值
function getCss(obj,name){
	obj = $(obj);
	if(obj.currentStyle) {
		return obj.currentStyle[name];
	}
	else {
		return document.defaultView.getComputedStyle(obj,null)[name];
	}
}

//飞机类
var Fly = function(){
	//飞机对应的dom元素
	this.dom = null;
	//飞机信息
	this.left = 0;
	this.top = 0;
	this.width = 0;
	this.height = 0;
	//可变大次数
	this.canBigCount = 1;
	//目前变大状态
	this.isBig = false;
	//移动状态
	this.isMove = false;
	//移动ID
	this.moveId = null;

	this.create();
}
Fly.prototype = {
	//移动位移
	movepx : 10,
	//移动速度
	moveSpeed : 30,
	//创建飞机dom
	create : function(){

		this.dom = document.createElement('div');
		this.dom.style.cssText = 'position:absolute;width:40px;height:15px;background:Yellow;';
		
		this.width = parseInt(this.dom.style.width,10);
		this.height = parseInt(this.dom.style.height,10);
	},
	//初始化飞机位置
	initPosition : function(gameWidth,gameHeight){
		
		this.left = (gameWidth-this.width)/2;
		this.top = gameHeight-this.height;
		this.dom.style.left = this.left + 'px';
		this.dom.style.top = this.top + 'px';
	},
	//更新位置
	update : function(){
		this.dom.style.left = this.left+'px';
		this.dom.style.top = this.top +'px';
	},
	//变大
	changeBig : function(){
		this.dom.style.width = (this.width*2) + 'px';
		this.width = this.width*2;
		this.isBig = true;
	},
	//还原
	changeNormal : function(){
		this.dom.style.width = (this.width/2) + 'px';
		this.width = this.width/2;
		this.isBig = false;
	},
	//键盘按下事件,e为event,gameWidth为游戏背景宽度
	keydown : function(e,gameWidth){
		switch(e.keyCode){
			//空格
			case 32:{
				//判断剩余变大次数与变大状态
				if(this.canBigCount > 0 && !this.isBig){
					
					var _this = this;
					//变大
					this.changeBig();
					//设置还原事件
					setTimeout(function(){
						_this.changeNormal();
					},5000);
					
					this.canBigCount -= 1;
					//刷新变大次数显示
					this.flashBigCount();
				}
				break;
			};
			//方向左
			case 37:{
				if(!this.isMove){
					this.isMove = true;
					this.move('left');
				}
				break;
			};
			//方向右
			case 39:{
				if(!this.isMove){
					this.isMove = true;
					this.move('right',gameWidth);
				}
				break;
			};
		}
	},
	//键盘释放事件
	keyup : function(e){
		
		if(e.keyCode == 37 || e.keyCode == 39){
			this.isMove = false;
			clearInterval(this.moveId);
		}
		
	},
	//飞机移动
	move : function(dir,gameWidth){
		
		_this = this;
		
		if(dir == 'left'){
			this.moveId = setInterval(function(){
				
				_this.left = _this.left-_this.movepx <=0?0:_this.left-_this.movepx;
				_this.update();
				
			},this.moveSpeed);
		}
		else{
			this.moveId = setInterval(function(){
				
				_this.left = _this.left+_this.movepx >=gameWidth-_this.width?gameWidth-_this.width:_this.left+_this.movepx;
				_this.update();
				
			},this.moveSpeed);
		}
		
	},
	//刷新变大次数显示，外部调用接口
	flashBigCount : function(){}
}

//子弹类,type为类型
var Bullet = function(type){
	//子弹dom元素
	this.dom = null;
	//子弹信息
	this.left = 0;
	this.top = 0;
	this.width = 0;
	this.height = 0;
	this.type = type;
	
	this.create();
}
Bullet.prototype = {
	//子弹类型与颜色映射表
	bullettype:{
		"good":"White",
		"bad":"Red",
		"big":"Blue"
	},
	//每次移动位移
	movepx : 10,
	//子弹速度
	movespeed : 50,
	//创建子弹dom
	create : function(){

		this.dom = document.createElement('div');
		this.dom.style.cssText = 'position:absolute;width:5px;height:5px;overflow:hidden;background:' + this.bullettype[this.type]+';';
		
		this.width = parseInt(this.dom.style.width,10);
		this.height = parseInt(this.dom.style.height,10);
	},
	//初始化子弹位置
	initPosition : function(gameWidth){
		
		this.left = parseInt(Math.random()*gameWidth+1,10);
		this.dom.style.left = this.left + 'px';
	},
	//子弹动画,height为游戏背景高度
	animation : function(height){
		
		var _this = this;
		//向下移动函数
		var downmove = function(){
			//更新位置
			_this.top = _this.top +  _this.movepx;
			_this.update();
			//判断子弹位置以及是否击中飞机
			if(_this.top < height && !_this.isBeatFly()){
				
				setTimeout(downmove,_this.movespeed);
			}
			else {
				//动画结束触发事件
				_this.onEnd();
			}
		}
		downmove();
	},
	//更新位置
	update : function(){
		this.dom.style.left = this.left+'px';
		this.dom.style.top = this.top +'px';
	},
	//判断子弹击中飞机否
	checkBeatFly :function(fly){
		if (this.left >= fly.left && this.left + this.width <= fly.left + fly.width) {
			if (this.top + this.height >= fly.top && this.top + this.height <= fly.top + fly.height) {
				return true;
			}
		}
		return false;
	},
	//动画结束触发事件，外部覆盖
	onEnd : function(){},
	//子弹是否击中飞机以及击中后处理事件，外部覆盖
	isBeatFly : function(){}
}

//游戏控制类
var Game = {
	//游戏背景dom元素
	gamePanel:null,
	//游戏背景宽度
	gameWidth : 0,
	//游戏背景高度
	gameHeight : 0,
	//子弹生成频率
	bulletHz : 200,
	//飞机
	fly : null,
	//分数
	score:0,
	//时间
	time : 61,
	//是否开始
	start : false,
	//初始化
	init : function(){
		
		this.initGamePanel();
		
		this.initFly();
		
		this.initBullet();
		
		this.startTime();
		
		this.start = true;
	},
	//初始化游戏背景数据
	initGamePanel : function(){
		this.gamePanel = $('panel');
		this.gameWidth = parseInt(getCss(this.gamePanel,'width'),10);
		this.gameHeight = parseInt(getCss(this.gamePanel,'height'),10);
		
		this.gamePanel.focus();
	},
	//初始化飞机
	initFly : function(){
		this.fly = new Fly();
		
		this.fly.initPosition(this.gameWidth,this.gameHeight);
		this.fly.flashBigCount = function(){
			$('canBigCount').innerHTML = this.canBigCount;
		}
		
		this.gamePanel.appendChild(this.fly.dom);
		//为body绑定键盘按下事件
		document.body.onkeydown = function(e){Game.flymove(e);};
		//为body绑定键盘释放事件
		document.body.onkeyup = function(e){Game.stopmove(e);};
	},
	//初始化子弹
	initBullet : function(){
		
		var _this = this;
		//生成子弹函数
		var process = function(){
			//随机数，决定生成加分或扣分子弹
			var random = parseInt(Math.random()*10+1,10);
			//随机数，决定生成增加变大子弹
			var addbig = parseInt(Math.random()*50+1,10);
			//新建一个子弹对象
			var bullet = new Bullet(random%3==0?'bad':addbig==28?'big':'good');
			
			bullet.initPosition(_this.gameWidth);
			//覆盖子弹动画结束事件
			bullet.onEnd = function(){
				_this.gamePanel.removeChild(this.dom);
				this.dom = null;
			}
			//覆盖子弹是否击中飞机以及击中后处理事件
			bullet.isBeatFly = function(){
			
				if (this.checkBeatFly(_this.fly)){
					_this.changeScore(this.type);
					return true;
				}
				return false;
			}
			
			_this.gamePanel.appendChild(bullet.dom);
			
			bullet.animation(_this.gameHeight);

			if(_this.time > 0)setTimeout(process,_this.bulletHz);
		}
		process();
	},
	//飞机移动
	flymove : function(e){
		e = e || window.event;

		this.fly.keydown(e,this.gameWidth);
	},
	//飞机停止
	stopmove : function(e){
		e = e || window.event;
		
		this.fly.keyup(e);
	},
	//改变分数
	changeScore : function(type){
		if (type == 'big') {
			this.fly.canBigCount += 1;
			this.fly.flashBigCount();
		}
		else {
			this.score += type == "bad" ? -100 : 100;
			$('score').innerHTML = this.score;
		}
	},
	//计时
	startTime : function(){
		if(this.time >0){
			var _this = this;
			this.time -= 1;
			$('time').innerHTML = this.time;
			setTimeout(function(){_this.startTime();},1000);
		}
		else {
			this.start = false;
		}
	},
	//重置
	reset : function(){
		this.gameWidth = 0;
		this.gameHeight = 0;
		this.fly && $('panel').removeChild(this.fly.dom);
		this.fly = null;
		this.movepx = 30;
		this.score = 0;
		$('score').innerHTML = this.score;
		this.time = 61;
		$('time').innerHTML = this.time-1;
		$('canBigCount').innerHTML = 1;
	}
}

function GameStart(){
	
	if (Game.start == false) {
		
		Game.reset();
		Game.init();
	}
}
